package com.excuse.excusebuilder.view.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.excuse.excusebuilder.R;
import com.excuse.excusebuilder.adapter.CommunityItemDetailAdapter;
import com.excuse.excusebuilder.base.BaseActivity;
import com.excuse.excusebuilder.bean.MessageLeft;
import com.excuse.excusebuilder.bean.Post;
import com.excuse.excusebuilder.impl.view.CommunityItemDetailInterface;
import com.excuse.excusebuilder.presenter.CommunityItemDetailPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by U-nookia on 2017/4/19.
 */

public class CommunityItemDetailActivity extends BaseActivity implements CommunityItemDetailInterface{
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.community_item_detail_title)
    TextView communityItemDetailTitle;
    @BindView(R.id.community_item_detail_content)
    TextView communityItemDetailContent;
    @BindView(R.id.community_item_detail_head)
    CircleImageView communityItemDetailHead;
    @BindView(R.id.community_item_detail_name)
    TextView communityItemDetailName;
    @BindView(R.id.community_item_detail_time)
    TextView communityItemDetailTime;
    @BindView(R.id.community_item_detail_thumb_up)
    ImageView communityItemDetailThumbUp;
    @BindView(R.id.item_detail)
    RelativeLayout itemDetail;
    @BindView(R.id.recycler_community_detail)
    RecyclerView recyclerCommunityDetail;

    private boolean thumbsUp = false;
    private Post post;
    private CommunityItemDetailAdapter adapter;
    private List<MessageLeft> messageLefts;

    private CommunityItemDetailPresenter presenter;

    @Override
    protected void onResume() {
        super.onResume();
        initLeftMsgs();
    }

    @Override
    protected void initData() {
        messageLefts = new ArrayList<>();
        presenter = new CommunityItemDetailPresenter(context,this);
        getDateFromIntent();
        initRecycler();
    }

    private void getDateFromIntent() {
        Intent intent = getIntent();
        post = (Post) intent.getSerializableExtra("item");
        initPost(post);
    }

    private void initRecycler() {
        recyclerCommunityDetail.setLayoutManager(new LinearLayoutManager(context));
    }

    private void initLeftMsgs() {
        presenter.initLeftMsgs(post);
    }

    private void initPost(Post post) {
        communityItemDetailTitle.setText(post.getTitle());
        communityItemDetailContent.setText(post.getContent());
        communityItemDetailName.setText(post.getUser().getUsername());
        communityItemDetailTime.setText(post.getCreatedAt());
        Glide.with(context).load(post.getUser().getAvatarUrl()).into(communityItemDetailHead);
        queryThumbsUp(post);
    }

    private void queryThumbsUp(Post post) {
        presenter.queryThumbsUp(post);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_community_item_detail;
    }

    @Override
    public void setThumbsUpImg() {
        communityItemDetailThumbUp.setImageResource(R.mipmap.thumbs_up_post_click);
        thumbsUp = true;
    }

    @Override
    public void haveSaveMsg(MessageLeft msg) {
        messageLefts.add(msg);
        adapter.notifyDataSetChanged();
    }

    private void addThumbsUpToBmob() {
        presenter.addThumbsUpToBmob(post);
    }

    @Override
    public void cancelThumbsUpImg() {
        communityItemDetailThumbUp.setImageResource(R.drawable.thumbs_up_post);
        thumbsUp = false;
    }

    private void deleteThumbsUpOnBmob() {
        presenter.deleteThumbsUpOnBmob(post);
    }

    @OnClick(R.id.community_item_detail_thumb_up)
    public void onViewClicked() {
        if (!thumbsUp) {
            setThumbsUpImg();
            addThumbsUpToBmob();
        } else {
            cancelThumbsUpImg();
            deleteThumbsUpOnBmob();
        }
    }

    @OnClick(R.id.item_detail)
    public void clickItemDetail(){
        new MaterialDialog.Builder(context)
                .content("回复 "+post.getUser().getUsername()+":")
                .inputType(InputType.TYPE_CLASS_TEXT)
                .input("输入回复内容", "", new MaterialDialog.InputCallback() {
                    @Override
                    public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
                        presenter.saveLeftMsgToBmob(post,input.toString());
                    }
                }).show();
    }

    @Override
    public void setAdapter(List<MessageLeft> msgs) {
        messageLefts = msgs;
        adapter = new CommunityItemDetailAdapter(messageLefts);
        recyclerCommunityDetail.setAdapter(adapter);
    }
}
